#include "isr.h"
#include "idt.h"

#define NULL 0

/*
 * Metodo para inicializar los descriptores de la IDT 
 */
void idtFill() {

	/* Lleno las entradas con un puntero al default */

	int i=0;
	while(i<256){

		idt[0].offset_0_15 = (unsigned short) ((unsigned int)(&_isr_default) & (unsigned int) 0xFFFF); \
		idt[0].segsel = (unsigned short) 0x0008; \
		idt[0].attr = (unsigned short) 0x8E00; \
		idt[0].offset_16_31 = (unsigned short) ((unsigned int)(&_isr_default) >> 16 & (unsigned int) 0xFFFF);

		i++;
	}

	/* Division por 0 */

	idt[0].offset_0_15 = (unsigned short) ((unsigned int)(&_isr0) & (unsigned int) 0xFFFF); \
	idt[0].segsel = (unsigned short) 0x0008; \
	idt[0].attr = (unsigned short) 0x8E00; \
	idt[0].offset_16_31 = (unsigned short) ((unsigned int)(&_isr0) >> 16 & (unsigned int) 0xFFFF);

	/* Double fault */

	idt[8].offset_0_15 = (unsigned short) ((unsigned int)(&_isr8) & (unsigned int) 0xFFFF); \
	idt[8].segsel = (unsigned short) 0x0008; \
	idt[8].attr = (unsigned short) 0x8E00; \
	idt[8].offset_16_31 = (unsigned short) ((unsigned int)(&_isr8) >> 16 & (unsigned int) 0xFFFF);

	/* Invalid TSS */

	idt[10].offset_0_15 = (unsigned short) ((unsigned int)(&_isr10) & (unsigned int) 0xFFFF); \
	idt[10].segsel = (unsigned short) 0x0008; \
	idt[10].attr = (unsigned short) 0x8E00; \
	idt[10].offset_16_31 = (unsigned short) ((unsigned int)(&_isr10) >> 16 & (unsigned int) 0xFFFF);

	/* Interrupcion del clock del IC */

	idt[32].offset_0_15 = (unsigned short) ((unsigned int)(&next_clock) & (unsigned int) 0xFFFF); \
	idt[32].segsel = (unsigned short) 0x0008; \
	idt[32].attr = (unsigned short) 0x8E00; \
	idt[32].offset_16_31 = (unsigned short) ((unsigned int)(&next_clock) >> 16 & (unsigned int) 0xFFFF);

	/* Interrupcion del clock del IC */

	idt[33].offset_0_15 = (unsigned short) ((unsigned int)(&_isr33) & (unsigned int) 0xFFFF); \
	idt[33].segsel = (unsigned short) 0x0008; \
	idt[33].attr = (unsigned short) 0x8E00; \
	idt[33].offset_16_31 = (unsigned short) ((unsigned int)(&_isr33) >> 16 & (unsigned int) 0xFFFF);

}

/* IDT */

idt_entry idt[255] = {};

/* Descriptor de la IDT (para cargar en IDTR) */

idt_descriptor IDT_DESC = {sizeof(idt)-1, (unsigned int)&idt};
